from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
import redis, re, random, string
from captcha.image import ImageCaptcha
from django.http import HttpResponse

# Create your views here.

class UserView(APIView):
    def get(self, request, user):
        if not re.findall(r'^[a-zA-Z]\w{5,15}$', user):
            return Response({
                'code': 400,
                'msg': '用户名格式不正确'
            })
        if User.objects.filter(user=user).count() > 0:
            return Response({
                'code': 401,
                'msg': '用户已存在'
            })
        return Response({
            'code': 200,
            'msg': '用户可用'
        })
    def post(self, request, user):
        name = request.data.get('user')
        pwd = request.data.get('pwd')
        phone = request.data.get('phone')
        if User.objects.filter(user=name).count() > 0:
            return Response({
                'code': 400,
                'msg': '用户已存在',
            })
        aa = User.objects.create(user=name, pwd=pwd, phone=phone)
        aa.save()
        return Response({
            'code': 200,
            'msg': '注册成功'
        })
class PhoneView(APIView):
    def get(self, request, phone):
        if not re.findall(r'^1[3-9]\d{9}$', phone):
            return Response({
                'code': 400,
                'msg': '手机号格式不正确'
            })
        if User.objects.filter(phone=phone).count() > 0:
            return Response({
                'code': 401,
                'msg': '手机号已存在'
            })
        code1 = random.randint(100000, 999999)
        r = redis.Redis(host='localhost', port=6379, db=5)
        r.set(phone, code1, ex=120)
        r.close()
        return Response({
            'code': 200,
            'msg': '验证码获取成功',
            'data': code1
        })
    def post(self, request, phone):
        user = request.data.get('user')
        pwd = request.data.get('pwd')
        if User.objects.filter(user=user).count() > 0:
            if User.objects.filter(user=user, pwd=pwd).count() > 0:
                return Response({
                    'code': 200,
                    'msg': '登录成功'
                })
            else:
                return Response({
                    'code': 401,
                    'msg': '密码错误'
                })
        else:
            return Response({
                'code': 400,
                'msg': '用户不存在'
            })
class TuView(APIView):
    def get(self, request, tu):
        code1 = ''.join(random.sample(string.digits, 3))
        r = redis.Redis(host='localhost', port=6379, db=5)
        r.set(tu, code1, ex=120)
        r.close()
        ret = ImageCaptcha()
        img = ret.generate(code1)
        return HttpResponse(img, 'image/png')
    def post(self, request, tu):
        code1 = request.data.get('UUID')
        r = redis.Redis(host='localhost', port=6379, db=5)
        code2 = r.get(tu)
        r.close()
        if code2:
            code2 = code2.decode()
        else:
            return Response({
                'code': 400,
                'msg': '图形验证码已过期'
            })
        if code1.lower().strip() == code2.lower().strip():
            return Response({
                'code': 200,
                'msg': '图形验证码成功',
            })
        else:
            return Response({
                'code': 401,
                'msg': '图形验证码错误'
            })

class LoginView(APIView):
    def get(self, request,):
        dataall = Student.objects.all()
        datalist = []
        for x in dataall:
            datalist.append({
                'id': x.id,
                'name': x.name,
                'gender': x.get_gender_display(),
                'age': x.age,
                'hao': x.hao,
            })
        return Response({
            'code': 200,
            'msg': '加载全部学生成功',
            'data': datalist
        })
    def post(self, request,):
        name = request.data.get('name')
        gender = request.data.get('gender')
        age = request.data.get('age')
        hao = request.data.get('hao')
        print(name, gender, age, hao)
        if Student.objects.filter(name=name).count() > 0:
            return Response({
                'code': 400,
                'msg': '用户已存在'
            })
        else:
            aa = Student.objects.create(name=name, gender=gender, age=age, hao=hao)
            aa.save()
            return Response({
                'code': 200,
                'msg': '添加成功'
            })
class XiuView(APIView):
    def get(self, request):
        aa = request.GET.get('id')
        data = Student.objects.get(id=aa)
        return Response({
            'id': data.id,
            'name': data.name,
            'gender': data.get_gender_display(),
            'age': data.age,
            'hao': data.hao,
        })
    def post(self, request):
        id = request.data.get('id')
        name = request.data.get('name')
        gender = request.data.get('gender')
        age = request.data.get('age')
        hao = request.data.get('hao')
        if gender == '男':
            gender = 1
        else:
            gender = 2
        print(id, name, gender, age, hao)
        Student.objects.filter(id=id).update(
            name=name, gender=gender, age=age, hao=hao
        )
        return Response({
            'code': 200,
            'msg': '修改成功'
        })
    def delete(self, request):
        id = request.data.get('idid')
        Student.objects.filter(id=id).delete()
        return Response({
            'code': 200,
            'msg': '删除成功'
        })
    def put(self, request):
        qq = request.data.get('cha')
        aa = Student.objects.filter(id=qq)
        aalist = []
        for x in aa:
            aalist.append({
                'id': x.id,
                'name': x.name,
                'gender': x.get_gender_display(),
                'age': x.age,
                'hao': x.hao,
            })
        return Response({
            'code': 200,
            'msg': '加载成功',
            'data': aalist
        })












